/* TicketLine.cpp
 * Author: mtreece
 * Date Started: 02/09/2010
 * Purpose:
 *  Represents an individual "line"
 *  on a Ticket object... consisting
 *  of 5 numbers and a powerball, whos
 *  ranges will be read from a .conf file
 *  at some point in the program's runtime.
 */

#include "TicketLine.h"

/* TODO: Memorize that all static variables,
 *       for whatever be the reason, must be
 *       declared in the class, "briefly", and
 *       initialized / "fully" declared outside
 *       the class.... also reasearch why!
 */
Definitions* TicketLine::definitions = NULL;

void TicketLine::setDefinitions(Definitions* d){
	definitions = d;
}

TicketLine::TicketLine(void){
	int toAdd;
	for(int i=0; i<5; i++){
		// fill the ticket with a *new* value
		while(!isNew(toAdd = TicketLine::definitions->getRandNum()));
		line[i] = toAdd;
	}

	/* sort(ARRAY, SIZE, TYPE_SORT) */
	IntArraySort::sort(line, 5, INSERT_SORT);

	pb = TicketLine::definitions->getRandPBNum();
}

TicketLine::
TicketLine(int n1, int n2, int n3, 
           int n4, int n5, int pb){
	line[0] = n1; line[1] = n2;
	line[2] = n3; line[3] = n4;
	line[4] = n5; this->pb = pb;
}

int TicketLine::isNew(int n){
	for(int i=0; i<5; i++){
		if(line[i] == n) return false;
	}
	return true;
}

MoneyWorth* TicketLine::getWorth(TicketLine* winner){
	return definitions->getWorth(winner, this);
}

char* TicketLine::toString(){
	/* Re-generate this each
	 * this each time it is called
	 * so as to avoid other
	 * programs' possible
	 * re-writes.
	 * TODO: Any better ways to
	 *       handle this?
	 */
	sprintf(string, 
	   "%-3d %-3d %-3d %-3d %-3d * %-3d",
	   line[0], line[1], line[2],
	   line[3], line[4], pb);
	return string;
}
